# -*- coding: utf-8 -*-
"""
Created on Thu Jul 25 21:09:23 2013

@author: tony
"""
import random
from copy import deepcopy

print "Welcome to Yeah, Right"

def generateGame(players):
    board = [[] for i in range(players)]
    chips = [11 for i in range(players)]
    deck = range(3,35)
    random.shuffle(deck)
    del deck[0:9]
    player = 0
    humanPlayer = random.randrange(players)
    draw = None
    return board, chips, deck, player, humanPlayer, draw



def printBoardStatus(board, deck, draw):
    for i,h in enumerate(board):
        print "Player %d: " % (i + 1), h, chips[i]
    print "Deck remaining: %d" % (len(deck))
    print "Drawn card: %d" % (draw)
    
def calcScore(board,chips):
    score = [0 for i in range(4)]
    for i,h in enumerate(board):
        if len(h) > 0:
            hand = sorted(h)
            diff = [hand[x]-hand[x-1] for x in range(1,len(hand))]
            score[i] = hand[0]
            for j,d in enumerate(diff):
                if d != 1:
                    score[i] += hand[j+1]        
        else:
            hand = h
            score[i] = 0
        score[i] -= chips[i]
    return score
        
def printScore(board,chips,score):
    print "Final Results:"
    for i,h in enumerate(board):
        hand = sorted(h)        
        print "Player %d: " % (i + 1), hand, chips[i], "Score: %d" % (score[i])
        
def playHuman(player,draw,ante):
    print "Card %0d, Chips %0d" % (draw,ante),
    inp = raw_input("Player %d: [T]ake or not?" % (player + 1))
    inp = inp.lower()
    if  inp == 't':
        take = True
    else:
        take = False
    return take
    
def playComputer(player,draw,ante,board,chips):
    boardT = deepcopy(board)
    boardT[player].append(draw)
    chipsT = list(chips)
    chipsT[player] += ante    
    scoreT = calcScore(boardT,chipsT)
    
    chipsN = list(chips)
    chipsN[player] -= 1   
    scoreN = calcScore(board,chipsN)
    
    if scoreT[player] < scoreN[player]:
        return True
    else:
        return False
    
# Play game
        
board, chips, deck, player, humanPlayer, draw = generateGame(4)

for i in xrange(len(deck)):
    # Set up round
    draw = deck.pop()
    taken = False
    ante = 0
    
    printBoardStatus(board,deck,draw)

    # Handle turns
    while not taken:
        if chips[player] > 0:
            take = None
        else:
            take = True
        while take is None:
            if player == humanPlayer:
                # play human
                take = playHuman(player,draw,ante)
            else:
                # play computer
                take = playComputer(player,draw,ante,board,chips)
        if not take:
            chips[player] -= 1
            ante += 1
            if player >= 3:
                player = 0
            else:
                player += 1
        else:
            board[player].append(draw)
            taken = True
            chips[player] += ante
            chipsTaken = ante
            ante = 0
    print "Player %d took the %d card and %d chips" % (player + 1, draw, chipsTaken)
        
score = calcScore(board,chips)
printScore(board,chips,score)
